#include "RecyclerViewAdapter.h"
#include "R.h"
#include <cdlog.h>
#include "conf_mgr.h"
#include "tools.h"
#include "wind_mgr.h"

RecyclerViewAdapter::RecyclerViewAdapter()
{
    // registerAdapterDataObserver(this);
    // modeDataList.emplace_back("@mipmap/pic_chushui_lengshui", "冰水1", "5~12", 350);
    // modeDataList.emplace_back("@mipmap/pic_chushui_lengshui", "冰水2", "5~12", 350);
    // modeDataList.emplace_back("@mipmap/pic_chushui_lengshui", "冰水3", "5~12", 350);
    // modeDataList.emplace_back("@mipmap/pic_chushui_lengshui", "冰水4", "5~12", 350);
    // modeDataList.emplace_back("@mipmap/pic_chushui_lengshui", "冰水5", "5~12", 350);
    // modeDataList.emplace_back("@mipmap/pic_chushui_lengshui", "冰水6", "5~12", 350);
    // modeDataList.emplace_back("@mipmap/pic_chushui_lengshui", "冰水7", "5~12", 350);
    // modeDataList.emplace_back("@mipmap/pic_chushui_lengshui", "冰水8", "5~12", 350);
    // modeDataList.emplace_back("@mipmap/pic_chushui_lengshui", "冰水9", "5~12", 350);
    // modeDataList.emplace_back("@mipmap/pic_chushui_lengshui", "冰水0", "5~12", 350);
    // modeDataList.emplace_back("@mipmap/pic_chushui_lengshui", "冰水090", "5~12", 350);
    // LOGE("-----------------------------------");
    modeDataList = d_fileLoad->getModeDataList();
    for (int i = 0; i < modeDataList.size(); i++)
    {
        LOGE("modeDataList = %s", modeDataList[i].toString().c_str());
    }
    
}

RecyclerViewAdapter::ViewHolder *RecyclerViewAdapter::onCreateViewHolder(ViewGroup *parent, int viewType)
{
    View*itemView=LayoutInflater::from(parent->getContext())->inflate("@layout/page_water_mode", nullptr);
    // LOGI("itemView=%p",itemView);
    if (itemView == nullptr)
    {
        LOGE("itemView = nullptr");
    }
    return new RecyclerViewAdapter::ViewHolder(itemView);
};

void RecyclerViewAdapter::onBindViewHolder(RecyclerView::ViewHolder &holder, int position)
{
    ModeData modeData = modeDataList.at(position);
    RecyclerViewAdapter::ViewHolder &rv = (RecyclerViewAdapter::ViewHolder &)holder;

    rv.vgUp->setOnClickListener([this, position](View &v){
        d_windMgr->showPage(PAGE_WATER_MODE_MODIFY, true, 1, position);
    });

    rv.modePic->setImageResource(std::string(modeData.imageDir));
    rv.modeName->setText(std::string(modeData.modeName));
    if (modeData.temperature == "实时")
    {
        rv.modeTempNum->setText(std::string(modeData.temperature));
    }else{
        rv.modeTempNum->setText(std::string(modeData.temperature) + std::string("℃"));
    }
    
    rv.modeChuShuiLiangNum->setText(std::to_string(modeData.chuShuiLiang) + std::string("mL"));

    if (position < 9 && position >= 0)
    {
        rv.modeNo->setText(std::to_string(position + 1).insert(0, 1, '0'));
    }
    else if (position >= 9)
    {
        rv.modeNo->setText(std::to_string(position + 1));
    }

    /**
     * 首中尾间距
     * 
     * 左右交换
     * otifyItemMoved(position,position+1);
     */
    // MarginLayoutParams
    if (position == 0)
    {
        
        // rv.modeDownLeft->Listener
        rv.modeDownLeft->setOnClickListener([this](View &){});
        rv.modeDownRight->setOnClickListener([this, position](View &){
            d_fileLoad->movedMode(position, position + 1);
            modeDataList = d_fileLoad->getModeDataList();
            notifyItemMoved(position, position + 1);
            notifyDataSetChanged();
        });
        rv.modeLeftBtn->setImageResource("@mipmap/icon_noleft");
        rv.modeRightBtn->setImageResource("@mipmap/icon_right");
        ((MarginLayoutParams *)rv.vg->getLayoutParams())->setMarginStart(48);
        ((MarginLayoutParams *)rv.vg->getLayoutParams())->setMarginEnd(0);
    }
    else if (position == getItemCount() - 1)
    {
        rv.modeDownLeft->setOnClickListener([this, position](View &){
            d_fileLoad->movedMode(position - 1, position);
            modeDataList = d_fileLoad->getModeDataList();
            notifyItemMoved(position - 1, position);
            notifyDataSetChanged();
        });
        rv.modeDownRight->setOnClickListener([this](View &){});
        rv.modeLeftBtn->setImageResource("@mipmap/icon_left");
        rv.modeRightBtn->setImageResource("@mipmap/icon_noright");
        ((MarginLayoutParams *)rv.vg->getLayoutParams())->setMarginStart(20);
        ((MarginLayoutParams *)rv.vg->getLayoutParams())->setMarginEnd(52);
    }
    else
    {
        rv.modeDownLeft->setOnClickListener([this, position, &rv](View &){
            LOGE("rv.getOldPosition() = %d", rv.getOldPosition());
            // LOGE("rv.getBindingAdapterPosition() = %d", rv.getBindingAdapterPosition());
            d_fileLoad->movedMode(position - 1, position);
            modeDataList = d_fileLoad->getModeDataList();
            notifyItemMoved(position - 1, position);
            notifyDataSetChanged();
        });
        rv.modeDownRight->setOnClickListener([this, position, &rv](View &){
            LOGE("rv.getOldPosition() = %d", rv.getOldPosition());
            // LOGE("rv.getBindingAdapterPosition() = %d", rv.getBindingAdapterPosition());
            d_fileLoad->movedMode(position, position + 1);
            modeDataList = d_fileLoad->getModeDataList();
            notifyItemMoved(position, position + 1);
            notifyDataSetChanged();
        });
        rv.modeLeftBtn->setImageResource("@mipmap/icon_left");
        rv.modeRightBtn->setImageResource("@mipmap/icon_right");
        ((MarginLayoutParams *)rv.vg->getLayoutParams())->setMarginStart(20);
        ((MarginLayoutParams *)rv.vg->getLayoutParams())->setMarginEnd(0);
    }
    
    /**
     * 删除按钮是否显示
     */
    if (!modeData.canDelete)
    {
        rv.modeCanDelete->setVisibility(View::GONE);
    } else {
        rv.modeCanDelete->setVisibility(View::VISIBLE);
    }
    

    // switch (position)
    // {
    // case 0:
    // case 1:
    // case 2:
    // {
    //     rv.modeCanDelete->setVisibility(View::GONE);
    //     LOGE("---------position--------%d", position);
    //     LOGE("---------modeData.imageDir--------%s", modeData.imageDir.c_str());
    // }
    // default:
    //     rv.modeCanDelete->setVisibility(View::VISIBLE);
    //     break;
    // }
    // rv.modePic->setOnClickListener([this,rv](View &v){
    //     // rv.getLayoutPosition()
    // });

    /**
     * 删除操作
     */
    rv.modeDownDelete->setOnClickListener([this,position](View &v){
        d_windMgr->showPopPage(POP_MODE_DELETE, [this, position](){
            d_fileLoad->deleteModeData(position);
            modeDataList = d_fileLoad->getModeDataList();
            notifyDataSetChanged();
        }, nullptr, position);
    });
}